package com.shunlin.paopaoku.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.shunlin.paopaoku.box2d.*;

//游戏按键类

public class GameButton extends Actor { //继承Actor类，Actor是Libgdx里边的演员类

    public ImageButton left; //图片按钮
    public ImageButton right;
    public ImageButton jump;

    private boolean moveToLeft = false;
    private boolean moveToRight = false;

    public GameButton() {
        init();
        addListener();
    }

    private void init() { //用于初始化资源
        left = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("button/left.jpg")))));
        right = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("button/right.jpg")))));
        jump = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("button/jump.jpg")))));
    }

    public void addListener() { //设置监听
        left.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                ClassManager.player.state = Player.State.LEFT; //改变player的状态
                moveToLeft = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                ClassManager.player.state = Player.State.IDLE;
                moveToLeft = false;
            }
        });

        right.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                ClassManager.player.state = Player.State.RIGHT;
                moveToRight = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                ClassManager.player.state = Player.State.IDLE;
                moveToRight = false;
            }
        });

        jump.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                if (Box2DContactListener.isOnGround) {
                    ClassManager.player.body.applyForceToCenter(0, ClassManager.player.JUMP, true); //使刚体受一个向上为400的力，实现跳跃效果
                    Music music = GameSound.makeMusic("sound/jump.mp3"); //播放jump音效
                    music.setVolume(1); //设置音乐的音量
                    music.play(); //播放音乐
                    System.out.println("jump");
                }
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

            }
        });
    }

    public void update() {
        if (moveToLeft == true) {
            ClassManager.player.body.applyForceToCenter(new Vector2(-10, 0), true); //使刚体受一个向左为10的力,实现向左移动
        } else if (moveToRight == true) {
            ClassManager.player.body.applyForceToCenter(new Vector2(10, 0), true);
        }
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        update();

        left.draw(batch, 0); //绘制按钮
        right.draw(batch, 0);
        jump.draw(batch, 0);
    }
}
